package com.weight.system.command.web;

import com.weight.system.command.PO.CommandInfo;
import com.weight.system.command.VO.CommandInfoVO;
import com.weight.system.command.service.CommandInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 指令表
 */
@RestController
@Slf4j

public class CommandInfoController {

	@Autowired
	private CommandInfoService commandInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询指令表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForCommandInfoList",method=RequestMethod.GET)
	public ModelAndView queryForCommandInfoList(){
			Map<Object, Object> map =new HashMap<>();
			map.put("status", 1);
			
			//2、查询指令表列表
			List<CommandInfo> commandInfoList = commandInfoService.findCommandInfoByT(map);
			log.info("commandInfoList={}",commandInfoList);
			List<CommandInfoVO> commandInfoVOList = commandInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			
			//3、返回前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("command/queryForCommandInfoList");
			modelAndView.addObject("commandInfoVOList",commandInfoVOList);
			
			return modelAndView;
	}

	/**
	 * 3.添加、修改指令表操作
	 * @param commandId
	 * @return
	 */
	@RequestMapping(value="/addCommandInfo")
	public ModelAndView addCommandInfo(@RequestParam(value="commandId",required=false) String commandId){
			//1、创建返回对象
			CommandInfoVO commandInfoVO = new CommandInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(commandId)) {
				Map<String, String> map=new HashMap<>();
				map.put("commandId", commandId);
				log.info("修改操作，查询ID：commandId={}",commandId);
				CommandInfo commandInfo = commandInfoService.findByCommandInfoId(map);
				this.changePoToVO(commandInfoVO,commandInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("command/addCommandInfo");
			modelAndView.addObject("commandInfoVO",commandInfoVO);
//			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改指令表操作
	 * @param commandInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveCommandInfo")
	public JsonResultUtil saveCommandInfo(@Valid CommandInfoVO commandInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,commandInfoVO);
			
			//2.获取id
			String commandId=commandInfoVO.getCommandId();
			CommandInfo commandInfo=new CommandInfo();
			BeanUtils.copyProperties(commandInfoVO,commandInfo);
			this.changeVOToPO(commandInfoVO,commandInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(commandId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("commandId", commandId);
				log.info("修改操作，查询ID：commandId={}",commandId);
				//查询是否存在
				commandInfoService.findByCommandInfoId(map);
				try{
					commandInfo.setUpdateBy("000");
					commandInfoService.updateCommandInfo(commandInfo);
					log.info("更新操作成功：commandInfo={}",commandInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					commandInfo.setCommandId(CommonUtils.getUUId());
					commandInfo.setCreateBy("000");
					commandInfo.setUpdateBy("000");
					commandInfo.setStatus(1);
					commandInfoService.insertCommandInfo(commandInfo);
					log.info("添加操作成功：commandInfo={}",commandInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除指令表操作
	 * @param commandId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByCommandInfoId")
	public JsonResultUtil deleteByCommandInfoId(@RequestParam(value="commandId",required=false) String commandId){
			//1.校验id
			CommonUtils.validateExistID(commandId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("commandId", commandId);
			commandInfoService.findByCommandInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除指令表入参，map={}",map);
			
			try{
				commandInfoService.upDelByCommandInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除指令表操作---批量删除
	 * @param commandIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByCommandInfoIdBatch")
	public JsonResultUtil deleteByCommandInfoIdBatch(@RequestParam(value="commandIdList",required=false) List commandIdList){
			//1.校验id
			CommonUtils.validateExistIDList(commandIdList);
			
			//2.校验数据库
			commandInfoService.findByCommandInfoIdList(commandIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("commandIdList", commandIdList);
			map.put("updateBy","000");
			log.info("批量删除指令表操作入参，map={}",map);
			
			try{
				commandInfoService.upDelByCommandInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}
	
	
	
	
	/**
	 * 2.查询指令表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForCommandInfoListDG",method=RequestMethod.GET)
	public String queryForCommandInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String commandMesg = request.getParameter("commandMesg");
			String commandName = request.getParameter("commandName");
			String commandType = request.getParameter("commandType");
			String commandGroup = request.getParameter("commandGroup");
			String commandArea = request.getParameter("commandArea");
			String areaType = request.getParameter("areaType");
			String areaOption = request.getParameter("areaOption");
			String status = request.getParameter("status");

			Map<Object, Object> map =new HashMap<>();
			map.put("commandMesg", commandMesg);
			map.put("commandName", commandName);
			map.put("commandType", commandType);
			map.put("commandGroup", commandGroup);
			map.put("commandArea", commandArea);
			map.put("areaType", areaType);
			map.put("areaOption", areaOption);
			map.put("status", status);

			
			//2、查询指令表列表
			List<CommandInfo> commandInfoList = commandInfoService.findCommandInfoByPage(page,rows,map);
			log.info("commandInfoList={}",commandInfoList);
			List<CommandInfoVO> commandInfoVOList = commandInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<CommandInfoVO> result = new PageResult<CommandInfoVO>(commandInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param commandInfoVO
	 * @param commandInfo
	 * @return
	 */
	public void changeVOToPO(CommandInfoVO commandInfoVO,CommandInfo commandInfo){
			BeanUtils.copyProperties(commandInfoVO,commandInfo);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param commandInfoVO
	 * @param commandInfo
	 * @return
	 */
	public void changePoToVO(CommandInfoVO commandInfoVO,CommandInfo commandInfo){
			BeanUtils.copyProperties(commandInfo,commandInfoVO);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param commandInfo
	 * @return
	 */
	public CommandInfoVO changePoToVO(CommandInfo commandInfo){
			CommandInfoVO commandInfoVO=new CommandInfoVO();
			BeanUtils.copyProperties(commandInfo,commandInfoVO);
			

			return commandInfoVO;
	}

	/**
	 * 3.查询指令表--详情页
	 * @param commandId
	 * @return
	 */
	@RequestMapping("/queryForCommandInfoDetail")
	public ModelAndView queryForCommandInfoDetail(@RequestParam(value="commandId",required=false) String commandId){
			//1.校验id
			CommonUtils.validateExistID(commandId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("commandId", commandId);
			log.info("查询指令表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			CommandInfo commandInfo = commandInfoService.findByCommandInfoId(map);
			
			CommandInfoVO commandInfoVO = changePoToVO(commandInfo);
			//3、返回前台
			log.info("查询指令表查询成功：commandInfo={}",commandInfo);
			modelAndView.setViewName("system/command/queryForCommandInfoDetail");
			modelAndView.addObject("commandInfoVO",commandInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	

}